class RoutesController < ApplicationController
  before_filter do |controller|
    controller.authorize_permissions({:blocked_permissions => (User::PermissionLevels - ["admin"])})
  end

  # GET /routes
  # GET /routes.json
  def index
    @title = "View Routes"
    
	session[:show_hidden_routes] ||= false
	@show_hidden = session[:show_hidden_routes]
	
	if @show_hidden
  	  @routes = Route.all
	else
	  @routes = Route.find_all_by_hidden false
	end
	  
    @page_results = @routes.paginate(:page => params[:page], :per_page => 10)

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @routes }
    end
  end

  # GET /routes/new
  # GET /routes/new.json
  def new
    @title = "New Route"
    @route = Route.new
	@route.stops << Stop.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @route }
    end
  end

  # GET /routes/1/edit
  def edit
    @route = Route.find(params[:id])
	@route_recurrence_rule = @route.route_recurrence_rules.new

	setup_edit @route
  end

  # POST /routes
  # POST /routes.json
  def create
    @route = Route.new(params[:route])

	@route.stops.each_with_index { |s,i|
		s.stop_number = i + 1
	}
	
    respond_to do |format|
      if @route.save
        flash[:notice] = 'Route was successfully created.'
        format.html { redirect_to(edit_route_path @route) }
        format.json  { render :json => @route, :status => :created, :location => @route }
      else
		@title = "New Route"
        format.html { render :action => "new" }
        format.json  { render :json => @route.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /routes/1
  # DELETE /routes/1.json
  def destroy
    @route = Route.find(params[:id])
    @route.hidden = !@route.hidden

	if @route.save
	  flash[:notice] = @route.hidden ? 'Route hidden' : 'Route un-hidden'
	else
	  flash[:notice] = @route.hidden ? "Route couoldn't be hidden" : "Route couldn't be un-hidden"
	end
	
    respond_to do |format|
      format.html { redirect_to(routes_url) }
      format.json  { head :ok }
    end
  end
  
  def toggle_hidden
    session[:show_hidden_routes] ||= false
	session[:show_hidden_routes] = !session[:show_hidden_routes]

	redirect_to :back
  end
  
  def create_rule
    @route_recurrence_rule = RouteRecurrenceRule.new(params[:route_recurrence_rule])

	@route = @route_recurrence_rule.route
	
    if @route_recurrence_rule.save
	  flash[:notice] = 'Courier successfully assigned to route.'
	  redirect_to edit_route_path(@route)
	else
	  setup_edit @route
      render :action => 'edit'
    end
  end
  
  def destroy_rule
    @route_recurrence_rule = RouteRecurrenceRule.find(params[:rule_id])
    
	@route = @route_recurrence_rule.route	
	@route_recurrence_rule.destroy
	flash[:notice] = 'Courier unassigned from route.'
	
	redirect_to edit_route_path(@route)
  end
  
private
  
  def setup_edit(route)
  	days = @route.route_recurrence_rules.map {|x|
	  x.days.split(',')
	}.flatten
	counts = {}
	days.each {|x|
	  counts[x] ||= 0
	  counts[x] += 1
	}
	@recurs_on = counts.sort.map { |k,v|
	  RouteRecurrenceRule::IToDay[k].to_s + (v > 1 ? " (x#{v})" : '')
	}
	
	@unassigned_couriers = Courier.all - @route.route_recurrence_rules.map {|x| x.courier}
    
	@title = "Edit Route: " + @route.name
  end
end
